package com.house.business.service.impl;

import com.house.business.entity.HousingResourcesInfo;
import com.house.business.vo.HousingResourcesInfoVo;
import com.house.business.mapper.HousingResourcesInfoMapper;
import com.house.business.service.HousingResourcesInfoService;
import com.house.framework.entity.AjaxResult;
import com.house.framework.service.impl.ServiceBaseImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.house.framework.annotations.Slave;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 房源表 服务实现类
 * </p>
 *
 * @since 2023-05-09
 */
@Service
public class HousingResourcesInfoServiceImpl extends ServiceBaseImpl<HousingResourcesInfoMapper, HousingResourcesInfo> implements HousingResourcesInfoService {

    @Autowired
    private HousingResourcesInfoMapper housingResourcesInfoMapper;

    /**
    * 新增房源表
    * @param entity
    * t_housing_resources_info新增数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object addHousingResourcesInfo(HousingResourcesInfo entity){
        entity.setCreateTime(new Date());
        housingResourcesInfoMapper.insert(entity);
        return AjaxResult.success();
    }

    /**
    * 修改房源表
    * @param entity
    * t_housing_resources_info修改数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object updateHousingResourcesInfo(HousingResourcesInfo entity){
        housingResourcesInfoMapper.updateById(entity);
        return AjaxResult.success();
    }

    /**
    * 取指定ID的房源表数据
    * @param id
    * t_housing_resources_info 主键ID
    * @return HousingResourcesInfoVo
    * 返回指定ID的房源表数据
    */
    @Slave
    @Override
    public HousingResourcesInfoVo getHousingResourcesInfoById(Long id) {
        return housingResourcesInfoMapper.get(id);
    }

    /**
    * 房源表分页取数
    * @param entityVo
    * t_housing_resources_info 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @Slave
    @Override
    public IPage<HousingResourcesInfoVo> getHousingResourcesInfoList(HousingResourcesInfoVo entityVo) {
        int pageIndex = entityVo.getPageIndex();
        int pageSize = entityVo.getPageSize();
        IPage page = new Page<HousingResourcesInfoVo>(pageIndex, pageSize);
        QueryWrapper<HousingResourcesInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(Objects.nonNull(entityVo.getRenovation()), "renovation", entityVo.getRenovation())
                .eq(Objects.nonNull(entityVo.getState()), "state", entityVo.getState())
                .eq(Objects.nonNull(entityVo.getPrice()), "price", entityVo.getPrice())
                .orderByDesc("create_time");
        return housingResourcesInfoMapper.page(page, queryWrapper);
    }

    /**
    * 删除指定ID的房源表数据
    * @param id
    * t_housing_resources_info 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteHousingResourcesInfoById(Long id){
        housingResourcesInfoMapper.delete(id);
    }

    /**
    * 逻辑删除指定ID的房源表数据
    * @param id
    * t_housing_resources_info 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void logicDeleteById(Long id){
        this.logicDelete(id, HousingResourcesInfoMapper.class, HousingResourcesInfo.class);
    }


    /**
    * 批量删除指定ID的房源表数据
    * @param ids
    * t_housing_resources_info 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchDelete(Long[] ids){
        housingResourcesInfoMapper.batchDelete(ids);
    }

    /**
    * 批量逻辑删除指定ID的房源表数据
    * @param ids
    * t_housing_resources_info 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchLogicDelete(Long[] ids){
        for(Long id : ids){
            this.logicDelete(id, HousingResourcesInfoMapper.class, HousingResourcesInfo.class);
        }
    }
}
